package bookpad;

import java.io.IOException;

import org.slim3.controller.Navigation;

import scenic3.annotation.ActionPath;
import scenic3.annotation.Default;
import scenic3.annotation.Page;
import scenic3.annotation.Var;
import bookpad.page.BasePage;
import bookpad.service.AmazonService;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Page("/")
public class FrontPage extends BasePage {

    @ActionPath("mode/{pageMode}")
    public Navigation mode(@Var("pageMode") String pageMode) {
        sessionScope("pageMode", pageMode);
        return forward("/index.jsp");
    }

    @ActionPath("logon")
    public Navigation logon() {
        if (getUser().isAuthentication()) {
            return redirect(basePath);
        } else {
            try {
                UserService userService = UserServiceFactory.getUserService();
                response.sendRedirect(userService.createLoginURL(request
                    .getRequestURI()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    @ActionPath("logout")
    public Navigation logout() {
        if (!getUser().isAuthentication()) {
            return redirect(basePath);
        } else {
            try {

                UserService userService = UserServiceFactory.getUserService();
                response.sendRedirect(userService.createLogoutURL(request
                    .getRequestURI()));
                getUser().setAuthentication(false);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    // /
    @Default
    public Navigation index() {
        sessionScope("pageMode", requestScope("mode"));

        AmazonService as = new AmazonService();
        requestScope("noticeList", as.getNoticeList());

        sessionScope("categorys", categorys);

        return forward("/index.jsp");
    }
}